package com.ddwl.common.exception;


import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;

/**
 * Function: 参数错误异常类<br/>
 * date: 2018年7月11日 下午2:36:12 <br/>
 *
 * @author zach
 * @since JDK 1.8
 */
public class ParamException extends GlobalException {
    private static final long serialVersionUID = 6021390821349937519L;

    private static final String msg = "请求参数异常";

    public ParamException(String message) {
        super(400, message);
    }

    public static void isTrue(boolean expression, String message) {
        if (expression) {
            throwException(message);
        }
    }

    public static void isTrue(boolean expression) {
        if (expression) {
            throwException(msg);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (!expression) {
            throwException(message);
        }
    }

    public static void isFalse(boolean expression) {
        if (!expression) {
            throwException(msg);
        }
    }

    public static void isNull(Object object, String message) {
        if (object == null) {
            throwException(message);
        }
    }

    public static void isNull(Object object, String message, Object... params) {
        if (object == null) {
            throwException(StrUtil.format(message, params));
        }
    }

    public static void isNotNull(Object object, String message) {
        if (object != null) {
            throwException(message);
        }
    }


    public static void isNull(Object obj) {
        isNull(obj, msg);
    }

    public static void isBlank(String text, String message) {
        if (!StringUtils.hasText(text)) {
            throwException(message);
        }
    }

    public static void isBlank(String text) {
        isBlank(text, msg);
    }

    private static void throwException(String msg) {
        throw new ParamException(msg);
    }
}
